;Copyright 2007-2010 Niko Carpenter and Tyler Spivey
;    This file is part of Linna Home Server.
;    Linna Home is free software: you can redistribute it and/or modify
;    it under the terms of the GNU General Public License as published by
;    the Free Software Foundation, either version 3 of the License, or
;    any later version.

;    Linna Home is distributed in the hope that it will be useful,
;    but WITHOUT ANY WARRANTY; without even the implied warranty of
;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;    GNU General Public License for more details.

;    You should have received a copy of the GNU General Public License
;    along with Linna Home.  If not, see http://www.gnu.org/licenses/.

#Include-once
#Include <date.au3>

func speakWithEngineJfw($text)
$jfw = objCreate("Jfwapi")
if not isObj($jfw) then return -1
$jfw.SayString($text,0)
endFunc
func speakWithEngineWe($text)
$we = objCreate("GWSpeak.Speak")
if not isObj($we) then return -1
$we.SpeakString($text)
endFunc
func speakWithEngineSapi($text)
global $sapiObj
if not isObj($sapiObj) then
$sapiObj = objCreate("SAPI.SPVoice")
if not isObj($sapiObj) then return -1
endIf
$sapiObj.Speak($text,1)
endFunc

Func _dkey($s_EncryptText, $s_EncryptPassword, $i_Encrypt,$i_EncryptLevel = 1)
	If $i_Encrypt <> 0 And $i_Encrypt <> 1 Then
		SetError(1)
		Return ''
	ElseIf $s_EncryptText = '' Or $s_EncryptPassword = '' Then
		SetError(1)
		Return ''
	Else
		If Number($i_EncryptLevel) <= 0 Or Int($i_EncryptLevel) <> $i_EncryptLevel Then $i_EncryptLevel = 1
		Local $v_EncryptModified
		Local $i_EncryptCountH
		Local $i_EncryptCountG
		Local $v_EncryptSwap
		Local $av_EncryptBox[256][2]
		Local $i_EncryptCountA
		Local $i_EncryptCountB
		Local $i_EncryptCountC
		Local $i_EncryptCountD
		Local $i_EncryptCountE
		Local $v_EncryptCipher
		Local $v_EncryptCipherBy
		If $i_Encrypt = 1 Then
			For $i_EncryptCountF = 0 To $i_EncryptLevel Step 1
				$i_EncryptCountG = ''
				$i_EncryptCountH = ''
				$v_EncryptModified = ''
				For $i_EncryptCountG = 1 To StringLen($s_EncryptText)
					If $i_EncryptCountH = StringLen($s_EncryptPassword) Then
						$i_EncryptCountH = 1
					Else
						$i_EncryptCountH = $i_EncryptCountH + 1
					EndIf
					$v_EncryptModified = $v_EncryptModified & Chr(BitXOR(Asc(StringMid($s_EncryptText, $i_EncryptCountG, 1)), Asc(StringMid($s_EncryptPassword, $i_EncryptCountH, 1)), 255))
				Next
				$s_EncryptText = $v_EncryptModified
				$i_EncryptCountA = ''
				$i_EncryptCountB = 0
				$i_EncryptCountC = ''
				$i_EncryptCountD = ''
				$i_EncryptCountE = ''
				$v_EncryptCipherBy = ''
				$v_EncryptCipher = ''
				$v_EncryptSwap = ''
				$av_EncryptBox = ''
				Local $av_EncryptBox[256][2]
				For $i_EncryptCountA = 0 To 255
					$av_EncryptBox[$i_EncryptCountA][1] = Asc(StringMid($s_EncryptPassword, Mod($i_EncryptCountA, StringLen($s_EncryptPassword)) + 1, 1))
					$av_EncryptBox[$i_EncryptCountA][0] = $i_EncryptCountA
				Next
				For $i_EncryptCountA = 0 To 255
					$i_EncryptCountB = Mod(($i_EncryptCountB + $av_EncryptBox[$i_EncryptCountA][0] + $av_EncryptBox[$i_EncryptCountA][1]), 256)
					$v_EncryptSwap = $av_EncryptBox[$i_EncryptCountA][0]
					$av_EncryptBox[$i_EncryptCountA][0] = $av_EncryptBox[$i_EncryptCountB][0]
					$av_EncryptBox[$i_EncryptCountB][0] = $v_EncryptSwap
				Next
				For $i_EncryptCountA = 1 To StringLen($s_EncryptText)
					$i_EncryptCountC = Mod(($i_EncryptCountC + 1), 256)
					$i_EncryptCountD = Mod(($i_EncryptCountD + $av_EncryptBox[$i_EncryptCountC][0]), 256)
					$i_EncryptCountE = $av_EncryptBox[Mod(($av_EncryptBox[$i_EncryptCountC][0] + $av_EncryptBox[$i_EncryptCountD][0]), 256) ][0]
					$v_EncryptCipherBy = BitXOR(Asc(StringMid($s_EncryptText, $i_EncryptCountA, 1)), $i_EncryptCountE)
					$v_EncryptCipher = $v_EncryptCipher & Hex($v_EncryptCipherBy, 2)
				Next
				$s_EncryptText = $v_EncryptCipher
			Next
		Else
			For $i_EncryptCountF = 0 To $i_EncryptLevel Step 1
				$i_EncryptCountB = 0
				$i_EncryptCountC = ''
				$i_EncryptCountD = ''
				$i_EncryptCountE = ''
				$v_EncryptCipherBy = ''
				$v_EncryptCipher = ''
				$v_EncryptSwap = ''
				$av_EncryptBox = ''
				Local $av_EncryptBox[256][2]
				For $i_EncryptCountA = 0 To 255
					$av_EncryptBox[$i_EncryptCountA][1] = Asc(StringMid($s_EncryptPassword, Mod($i_EncryptCountA, StringLen($s_EncryptPassword)) + 1, 1))
					$av_EncryptBox[$i_EncryptCountA][0] = $i_EncryptCountA
				Next
				For $i_EncryptCountA = 0 To 255
					$i_EncryptCountB = Mod(($i_EncryptCountB + $av_EncryptBox[$i_EncryptCountA][0] + $av_EncryptBox[$i_EncryptCountA][1]), 256)
					$v_EncryptSwap = $av_EncryptBox[$i_EncryptCountA][0]
					$av_EncryptBox[$i_EncryptCountA][0] = $av_EncryptBox[$i_EncryptCountB][0]
					$av_EncryptBox[$i_EncryptCountB][0] = $v_EncryptSwap
				Next
				For $i_EncryptCountA = 1 To StringLen($s_EncryptText) Step 2
					$i_EncryptCountC = Mod(($i_EncryptCountC + 1), 256)
					$i_EncryptCountD = Mod(($i_EncryptCountD + $av_EncryptBox[$i_EncryptCountC][0]), 256)
					$i_EncryptCountE = $av_EncryptBox[Mod(($av_EncryptBox[$i_EncryptCountC][0] + $av_EncryptBox[$i_EncryptCountD][0]), 256) ][0]
					$v_EncryptCipherBy = BitXOR(Dec(StringMid($s_EncryptText, $i_EncryptCountA, 2)), $i_EncryptCountE)
					$v_EncryptCipher = $v_EncryptCipher & Chr($v_EncryptCipherBy)
				Next
				$s_EncryptText = $v_EncryptCipher
				$i_EncryptCountG = ''
				$i_EncryptCountH = ''
				$v_EncryptModified = ''
				For $i_EncryptCountG = 1 To StringLen($s_EncryptText)
					If $i_EncryptCountH = StringLen($s_EncryptPassword) Then
						$i_EncryptCountH = 1
					Else
						$i_EncryptCountH = $i_EncryptCountH + 1
					EndIf
					$v_EncryptModified = $v_EncryptModified & Chr(BitXOR(Asc(StringMid($s_EncryptText, $i_EncryptCountG, 1)), Asc(StringMid($s_EncryptPassword, $i_EncryptCountH, 1)), 255))
				Next
				$s_EncryptText = $v_EncryptModified
			Next
		EndIf
		Return $s_EncryptText
	EndIf
EndFunc

Func _GetOsLang()
$langs = "0436|Afrikaans"&@CRLF&"041c|Albanian"&@CRLF&"0401|Arabic_Saudi_Arabia"&@CRLF&"0801|Arabic_Iraq"&@CRLF&"0c01|Arabic_Egypt"&@CRLF&"1001|Arabic_Libya"&@CRLF&"1401|Arabic_Algeria"&@CRLF&"1801|Arabic_Morocco"&@CRLF&"1c01|Arabic_Tunisia"&@CRLF&"2001|Arabic_Oman"&@CRLF&"2401|Arabic_Yemen"&@CRLF&"2801|Arabic_Syria"&@CRLF&"2c01|Arabic_Jordan"&@CRLF&"3001|Arabic_Lebanon"&@CRLF&"3401|Arabic_Kuwait"&@CRLF&"3801|Arabic_UAE"&@CRLF&"3c01|Arabic_Bahrain"&@CRLF&"4001|Arabic_Qatar"&@CRLF&"042b|Armenian"&@CRLF&"042c|Azeri_Latin"&@CRLF&"082c|Azeri_Cyrillic"&@CRLF&"042d|Basque"&@CRLF&"0423|Belarusian"&@CRLF&"0402|Bulgarian"&@CRLF&"0403|Catalan"&@CRLF&"0404|Chinese_Taiwan"&@CRLF&"0804|Chinese_PRC"&@CRLF&"0c04|Chinese_Hong_Kong"&@CRLF&"1004|Chinese_Singapore"&@CRLF&"1404|Chinese_Macau"&@CRLF&"041a|Croatian"&@CRLF&"0405|Czech"&@CRLF&"0406|Danish"&@CRLF&"0413|Dutch_Standard"&@CRLF&"0813|Dutch_Belgian"&@CRLF&"0409|English_United_States"&@CRLF&"0809|English_United_Kingdom"&@CRLF&"0c09|English_Australian"&@CRLF&"1009|English_Canadian"&@CRLF&"1409|English_New_Zealand"&@CRLF&"1809|English_Irish"&@CRLF&"1c09|English_South_Africa"&@CRLF&"2009|English_Jamaica"&@CRLF&"2409|English_Caribbean"&@CRLF&"2809|English_Belize"&@CRLF&"2c09|English_Trinidad"&@CRLF&"3009|English_Zimbabwe"&@CRLF&"3409|English_Philippines"&@CRLF&"0425|Estonian"&@CRLF&"0438|Faeroese"&@CRLF&"0429|Farsi"&@CRLF&"040b|Finnish"&@CRLF&"040c|French_Standard"&@CRLF&"080c|French_Belgian"&@CRLF&"0c0c|French_Canadian"&@CRLF&"100c|French_Swiss"&@CRLF&"140c|French_Luxembourg"&@CRLF&"180c|French_Monaco"&@CRLF&"0437|Georgian"&@CRLF&"0407|German_Standard"&@CRLF&"0807|German_Swiss"&@CRLF&"0c07|German_Austrian"&@CRLF&"1007|German_Luxembourg"&@CRLF&"1407|German_Liechtenstei"&@CRLF&"408|Greek"&@CRLF&"040d|Hebrew"&@CRLF&"0439|Hindi"&@CRLF&"040e|Hungarian"&@CRLF&"040f|Icelandic"&@CRLF&"0421|Indonesian"&@CRLF&"0410|Italian_Standard"&@CRLF&"0810|Italian_Swiss"&@CRLF&"0411|Japanese"&@CRLF&"043f|Kazakh"&@CRLF&"0457|Konkani"&@CRLF&"0412|Korean"&@CRLF&"0426|Latvian"&@CRLF&"0427|Lithuanian"&@CRLF&"042f|Macedonian"&@CRLF&"043e|Malay_Malaysia"&@CRLF&"083e|Malay_Brunei_Darussalam"&@CRLF&"044e|Marathi"&@CRLF&"0414|Norwegian_Bokmal"&@CRLF&"0814|Norwegian_Nynorsk"&@CRLF&"0415|Polish"&@CRLF&"0416|Portuguese_Brazilian"&@CRLF&"0816|Portuguese_Standard"&@CRLF&"0418|Romanian"&@CRLF&"0419|Russian"&@CRLF&"044f|Sanskrit"&@CRLF&"081a|Serbian_Latin"&@CRLF&"0c1a|Serbian_Cyrillic"&@CRLF&"041b|Slovak"&@CRLF&"0424|Slovenian"&@CRLF&"040a|Spanish_Traditional_Sort"&@CRLF&"080a|Spanish_Mexican"&@CRLF&"0c0a|Spanish_Modern_Sort"&@CRLF&"100a|Spanish_Guatemala"&@CRLF&"140a|Spanish_Costa_Rica"&@CRLF&"180a|Spanish_Panama"&@CRLF&"1c0a|Spanish_Dominican_Republic"&@CRLF&"200a|Spanish_Venezuela"&@CRLF&"240a|Spanish_Colombia"&@CRLF&"280a|Spanish_Peru"&@CRLF&"2c0a|Spanish_Argentina"&@CRLF&"300a|Spanish_Ecuador"&@CRLF&"340a|Spanish_Chile"&@CRLF&"380a|Spanish_Uruguay"&@CRLF&"3c0a|Spanish_Paraguay"&@CRLF&"400a|Spanish_Bolivia"&@CRLF&"440a|Spanish_El_Salvador"&@CRLF&"480a|Spanish_Honduras"&@CRLF&"4c0a|Spanish_Nicaragua"&@CRLF&"500a|Spanish_Puerto_Rico"&@CRLF&"0441|Swahili"&@CRLF&"041d|Swedish"&@CRLF&"081d|Swedish_Finland"&@CRLF&"0449|Tamil"&@CRLF&"0444|Tatar"&@CRLF&"041e|Thai"&@CRLF&"041f|Turkish"&@CRLF&"0422|Ukrainian"&@CRLF&"0420|Urdu"&@CRLF&"0443|Uzbek_Latin"&@CRLF&"0843|Uzbek_Cyrillic"&@CRLF&"042a|Vietnamese"&@CRLF


local $lang = "unknown"
if StringInStr($langs,@OSLang&"|") Then
$lang = StringTrimLeft($langs,StringInStr($langs,@OSlang)+StringLen(@OSLang))
$lang = StringLeft($Lang,StringInStr($lang,@CRLF)-1)
EndIf
return $lang
EndFunc

Func _ShowInternalIPs()
$ret = ""
if @IPaddress1 <> "0.0.0.0" Then $ret = $ret&" "&@IPaddress1
if @IPaddress2 <> "0.0.0.0" Then $ret = $ret&" "&@IPaddress2
if @IPaddress3 <> "0.0.0.0" Then $ret = $ret&" "&@IPaddress3
if @IPaddress4 <> "0.0.0.0" Then $ret = $ret&" "&@IPaddress4
$ret = StringTrimLeft($ret,1)
return $ret
EndFunc

func _parsequotes($str)
$strtrim = 0
local $ret[1]
$str = StringReplace($str,"""""",@CRLF)
While 1
$cstr = StringTrimLeft($str,$strtrim)
$strl = 0
$strr = 0
if StringInStr($cstr,"""") Then
$strl = StringInStr($cstr,"""")+1
$strr = StringInStr(StringTrimLeft($cstr,$strl-1),"""")-1
if $strr > 0 Then
$strtrim = $strtrim + $strl + $strr
redim $ret[ubound($ret)+1]
$ret[ubound($ret)-1] = StringMid($cstr,$strl,$strr)
Else
seterror(1)
return ""
EndIf
Else
ExitLoop
EndIf
Wend
$ret[0] = ubound($ret)-1
for $i = 1 to $ret[0]
$ret[$i] = StringReplace($ret[$i],@CRLF,"""")
Next
return $ret
EndFunc

Func _StringToInt($str)
local $int = 0
local $len = StringLen($str)
for $i = 1 to $len
$val = asc(stringRight($str,1))
$int = $int + ($val*256^($i-1))
$str = StringTrimRight($str,1)
Next
return $int
EndFunc


